package cn.gson.crmdemo.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

@Component
public class CorsFilter implements Filter {  
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {  
    	HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpServletRequest request = (HttpServletRequest) servletRequest;

		String origin = request.getHeader("Origin");
		response.setHeader("Access-Control-Allow-Origin", origin);
		response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.setHeader("Access-Control-Allow-Headers", "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE");
		// response.setHeader("Access-Control-Allow-Headers",
		// "x-requested-with,Authorization");
		response.setHeader("Access-Control-Allow-Credentials", "true");
		String method = request.getMethod();
		if (method.equalsIgnoreCase("OPTIONS")) {
			((HttpServletResponse) servletResponse).setStatus(HttpServletResponse.SC_OK);
			response.getWriter().write("OPTIONS returns OK");
		} else {
			filterChain.doFilter(servletRequest, servletResponse);
		}

    }  
    public void init(FilterConfig filterConfig) {}  
    public void destroy() {}  
}  